package com.wj.facedetect.util;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by tangkun on 2017/9/29.
 */

public class TimeUtils {
    public static final String DATE_FORMAT_YYYYMMDD = "yyyy.MM.dd";
    public static final String DATE_FORMAT_YYYYMMDD_ = "yyyy-MM-dd";
    public static final String DATE_FORMAT_YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT_YYYYMMDDHHMM = "yyyy-MM-dd HH:mm";
    public static final String DATE_FORMAT_MMDDHHMM = "MM-dd HH:mm";
    public static final String DATE_FORMAT_YYYYMMDDHHMMSS1 = "yyyyMMdd_HH_mm_ss";
    public static final String DATE_FORMAT_YYYYMMDDHHMMSS2 = "yyyy.MM.dd HH:mm:ss";
    public static final String DATE_FORMAT_YY = "yy年";
    public static final String DATE_FORMAT_YYYY = "yyyy";
    public static final String DATE_FORMAT_MMDD = "MM 月 dd";
    public final static String INVALID = "--";

    /**
     * 获取传入格式形式的时间字符串
     *
     * @param time
     * @param type 转换成的类型
     * @return
     */
    public static String getFormatTimeByType(long time, String type) {
        if (time <= 0) {
            return INVALID;
        }
        Date date = new Date(time);
        return getFormatTimeByType(date, type);
    }

    /**
     * 获取传入格式形式的时间字符串
     *
     * @param date
     * @param type 转换成的类型
     * @return
     */
    public static String getFormatTimeByType(Date date, String type) {
        if (date == null) {
            return INVALID;
        }
        SimpleDateFormat formatter = new SimpleDateFormat(type, Locale.CHINESE);
        return formatter.format(date);
    }

    /**
     * 获取Java时间戳
     *
     * @param time 字符串时间
     * @param type 对应的时间类型
     * @return 时间戳ms
     */
    public static long getTimestamp(String time, String type) {
        if (TextUtils.isEmpty(time)) {
            return Long.MAX_VALUE;
        }
        SimpleDateFormat formatter = new SimpleDateFormat(type, Locale.getDefault());
        Date date = null;
        try {
            date = formatter.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }

    /**
     * 获取星期几，在最近一天内显示昨天，今天，明天
     *
     * @param time
     * @return 形如星期一或昨天，今天，明天
     */
    public static String getWeekHasLately(long time) {
        String week;
        if (isDiffDay(time, -1)) {//昨天
            week = "昨天";
        } else if (isDiffDay(time, 0)) {//今天
            week = "今天";
        } else if (isDiffDay(time, 1)) {//明天
            week = "明天";
        } else {
            week = getFormatTimeByType(new Date(time), "EEEE");
        }
        return week;
    }

    private static boolean isDiffDay(long time, int dayDiff) {
        String todayString = getFormatTimeByType(System.currentTimeMillis(), DATE_FORMAT_YYYYMMDD);
        long temp = time + ((-1l) * dayDiff * 24l * 60l * 60l * 1000l);
        String tempString = getFormatTimeByType(temp, DATE_FORMAT_YYYYMMDD);
        return todayString.equals(tempString);
    }
}
